<?php

namespace App\Http\Controllers;

use App\Http\Models\CarModel;
use App\Model\CarVin;
use App\Model\CarOil;
use App\Model\CarModelObjectInfo;
use App\Model\CarVinLog;
use App\Providers\VinServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CarController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    /**
     * 获取brands
     */
    public function brands(){

        $brands = CarModel::brands();

        echo json_encode( $brands );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function brand( $id ){

        $brand = CarModel::brand($id);

        echo json_encode( $brand );
    }

    /**
     * 获取brand 根据brand_id
     */
    public function brand_edit( Request $request, $id ){

        $post = $request->all();
        $params = $post;
        $brand = CarModel::brand_edit($id, $params);
        echo json_encode( $brand );
    }

    /**
     * 获取model
     */
    public function models($brand_id){

        $models = CarModel::getModelsByBrandId($brand_id);
        echo json_encode($models);

    }

    public function modelObjects( $model_id ){

        $modelObjects = CarModel::modelObjects( $model_id );
        echo json_encode( $modelObjects );

    }

    public function objectInfo( $mi_key ){

        $objectInfo = CarModel::modelObjectInfo( $mi_key );

        //todo 如果oil_id为空，
        // 1.根据car_oil_exchange_special,car_oil_exchange两张表进行对应
        // 2.把对应到的oil_id 插入car_model_object_info表中
        if( empty( $objectInfo->oil_id ) ){
            $new_oil = CarOil::getOilByRule( $mi_key, $objectInfo->oil );
            if( !empty( $new_oil ) ){
                CarModelObjectInfo::setOilId( $mi_key, $new_oil['id']);
            }
        }else{
            $new_oil = CarOil::where('id', $objectInfo->oil_id )->first()->toarray();

        }

        if( !empty( $new_oil ) ){
            $objectInfo->old_oil = $objectInfo->oil;
            $objectInfo->old_oil_img_url = $objectInfo->oil_img_url;

            $objectInfo->oil = $new_oil['name'];
            $objectInfo->oil_id = $new_oil['id'];
            $objectInfo->oil_img_url = $new_oil['img_url'];
            $objectInfo->link = $new_oil['link'];

        }
        echo json_encode( $objectInfo );

    }

    public function objectInfoFromVin( Request $request, $vin ){

//        $car_vin = CarVin::info( $vin );
//        $car_vin = CarVin::info_new( $vin );
        $car_vin = CarVin::info_linghang( $vin );

        //todo 如果$car_vin不为空，and  oil_id is empty
        //todo 如果数据来源为领航， (全条件)查询对应的car_model_object_info, 再拿到oil_id
        //todo 如果数据来源为聚合， (部分条件)查询对应的car_model_object_info, 再拿到oil_id
        $oil_id = 0;
        if( !empty($car_vin) ){

            if( empty( $car_vin['oil_id'] ) ){
                if( $car_vin['source'] == 'linghang'){
                    $objectInfo = CarVin::findSameObjectInfo( $car_vin );  //找相同的car_model_object_info
                }else{
                    $objectInfo = CarVin::findlikedObjectInfo( $car_vin ); // 找相似的car_model_object_info

                }

                if( !empty( $objectInfo ) ){

                    if( empty( $objectInfo->oil_id ) ){
                        $new_oil = CarOil::getOilByRule( $objectInfo->mi_key, $objectInfo->oil );
                        if( !empty( $new_oil ) ){
                            $oil_id = $new_oil['id'];
                            //todo 如果表 car_model_object_info 中的oil_id 为空，插入该值
                            CarModelObjectInfo::setOilId( $objectInfo->mi_key, $new_oil['id']);
                        }
                    }else{
                        $oil_id = $objectInfo->oil_id;
                    }
                    //todo 如果表 car_vin 中的oil_id 为空，插入该值
                    if( !empty($oil_id) ){
                        $data = [
                            'oil_id'        => $oil_id,
                            'oil_regional'  => !empty( $car_vin['oil_regional'] )
                                ? $car_vin['oil_regional'] : $objectInfo->oil_regional,
                            'oil_change'    => !empty( $car_vin['oil_change'] )
                                ? $car_vin['oil_change'] : $objectInfo->oil_change,
                            'gearbox_lv'    => !empty( $car_vin['gearbox_lv'] )
                                ? $car_vin['gearbox_lv'] : $objectInfo->gearbox_lv,
                        ];
                        $car_vin['oil_regional']    = $data['oil_regional'];
                        $car_vin['oil_change']      = $data['oil_change'];
                        $car_vin['gearbox_lv']      = $data['gearbox_lv'];
                        CarVin::editOil( $vin, $data );
                    }
                }


            }else{
                $oil_id = $car_vin['oil_id'];
            }

            //todo 如果vin码来源为juhe2，查询是否有多条对应结果
            if( $car_vin['source']=='juhe2' ){
                $car_vin['extra_data'] = CarVin::getJuhe2ExtraByGroup( $car_vin['vin'] );
            }


        }


        //todo 根据oil_id 获取到润滑油
        if( !empty( $oil_id ) ){
            $new_oil = CarOil::where('id', $oil_id )->first()->toarray();

            $car_vin['oil']    = $new_oil['name'];
            $car_vin['oil_id']      = $new_oil['id'];
            $car_vin['oil_img_url'] = $new_oil['img_url'];
            $car_vin['link'] = $new_oil['link'];

        }else{ //如果没有匹配上油品，使用“请联系格斯特润滑油技术工程师”（oil id为 11）
            $new_oil = CarOil::where('id', 11 )->first()->toarray();

            $car_vin['oil']    = $new_oil['name'];
            $car_vin['oil_id']      = $new_oil['id'];
            $car_vin['oil_img_url'] = $new_oil['img_url'];
            $car_vin['link'] = $new_oil['link'];
        }

        //todo 添加vin码查询记录
        CarVinLog::record( $vin );
        echo json_encode( $car_vin );

    }

    /**
     * 测试龙蟠数据抓取
     */
    public function testLopal(){

//        $vin = 'LFV2A2159B3503936';
        $vin = 'LSGPC54U5DF150079';
        VinServiceProvider::lopalInfo( $vin );
    }

    // 特殊数据的润滑油对应关系
    public function insertData(){

        $data = array(
            "DCTF MIA120114A001",
            "DCTF MIA080187A001",
            "建议使用原厂专用油 MIB020860A001",
            "建议使用原厂专用油 MIB040857A001",
            "建议使用原厂专用油 MIB040857A002",
            "建议使用原厂专用油 MIB040858A001",
            "建议使用原厂专用油 MIB040858A002",
            "建议使用原厂专用油 MIB040859A002",
            "建议使用原厂专用油 MIB040859A005",
            "建议使用原厂专用油 MIB040859A001",
            "建议使用原厂专用油 MIB040861A001",
            "建议使用原厂专用油 MIB040861A002",
            "建议使用原厂专用油 MIB010855A001",
            "建议使用原厂专用油 MIB010856A001",
            "建议使用原厂专用油 MIB010856A003",
            "建议使用原厂专用油 MIB011328A001",
            "建议使用原厂专用油 MIB011328A002",
            "建议使用原厂专用油 MIB011328A003",
            "建议使用原厂专用油 MIB011328A004",
            "建议使用原厂专用油 MIB011279A001",
            "建议使用原厂专用油 MIB011279A002",
            "建议使用原厂专用油 MIB011279A003",
            "建议使用原厂专用油 MIB031280A001",
            "建议使用原厂专用油 MIB031280A005",
            "建议使用原厂专用油 MIB031280A006",
            "建议使用原厂专用油 MIB031280A007",
            "建议使用原厂专用油 MIB031280A008",
            "建议使用原厂专用油 MIB031281A001",
            "建议使用原厂专用油 MIB031281A002",
            "建议使用原厂专用油 MIB031281A003",
            "建议使用原厂专用油 MIB040794A001",
            "75-90 GL-4 MIH021203B001",
            "75-90 GL-4 MIH021204A001",
            "75-90 GL-4 MIH021205A001",
            "建议使用原厂专用油 MIB040534A004",
            "建议使用原厂专用油 MIB040534A001",
            "7AT MIB010286A001",
            "建议使用原厂专用油 MIC015915A001",
            "建议使用原厂专用油 MIC015915A002",
            "建议使用原厂专用油 MIF042150A001",
            "75-90 GL-4 MIH021203B001",
            "75-90 GL-4 MIH021204A001",
            "75-90 GL-4 MIH021205A001",
            "建议使用原厂专用油 MIG012694A001",
            "建议使用原厂专用油 MIG012694A002",
            "建议使用原厂专用油 MIG012694A005",
            "建议使用原厂专用油 MIG012694A006",
            "建议使用原厂专用油 MIH032850A001",
            "建议使用原厂专用油 MIH012949A002",
            "建议使用原厂专用油 MIH012949A003",
            "建议使用原厂专用油 MIH022950A001",
            "建议使用原厂专用油 MIH022950A002",
            "建议使用原厂专用油 MIH022950A003",
            "建议使用原厂专用油 MIJ013249A001",
            "建议使用原厂专用油 MIJ013229A001",
            "建议使用原厂专用油 MIJ013229A002",
            "建议使用原厂专用油 MIJ013230A001",
            "建议使用原厂专用油 MIJ023228A001",
            "6AT MIL083890A002",
            "6AT MIL083889A001",
            "建议使用原厂专用油 MIL043841A001",
            "建议使用原厂专用油 MIL043841A002",
            "建议使用原厂专用油 MIC044447A002",
            "建议使用原厂专用油 MIC044447A001",
            "建议使用原厂专用油 MIR044818A001",
            "建议使用原厂专用油 MIR044818A002",
            "建议使用原厂专用油 MIR044818A003",
            "建议使用原厂专用油 MIR044818A004",
            "建议使用原厂专用油 MIR044818A005",
            "建议使用原厂专用油 MIR044818A016",
            "建议使用原厂专用油 MIV044546A003",
            "建议使用原厂专用油 MIV044546A001",
            "建议使用原厂专用油 MIV044546A002",
            "建议使用原厂专用油 MIR044818A001",
            "建议使用原厂专用油 MIR044818A002",
            "建议使用原厂专用油 MIR044818A003",
            "建议使用原厂专用油 MIR044818A004",
            "建议使用原厂专用油 MIR044818A005",
            "建议使用原厂专用油 MIR044818A016",
            "建议使用原厂专用油 MIT015028A001",
            "建议使用原厂专用油 MIT015029A001",
            "建议使用原厂专用油 MIT015030A001",
            "建议使用原厂专用油 MIT015031A001",
            "建议使用原厂专用油 MIT015031A003",
            "建议使用原厂专用油 MIT015032A001",
            "建议使用原厂专用油 MIT015032A003",
            "建议使用原厂专用油 MIT015033A001",
            "建议使用原厂专用油 MIT015034A001",
            "建议使用原厂专用油 MIT015035A001",
            "建议使用原厂专用油 MIT015035A003",
            "建议使用原厂专用油 MIT015036A001",
            "建议使用原厂专用油 MIT015037A001",
            "建议使用原厂专用油 MIT015038A001",
            "建议使用原厂专用油 MIT015038A003",
            "建议使用原厂专用油 MIT015039A001",
            "建议使用原厂专用油 MIT015040A001",
            "建议使用原厂专用油 MIT015041A001",
            "建议使用原厂专用油 MIT015042A001",
            "建议使用原厂专用油 MIT015042A003",
            "建议使用原厂专用油 MIT015026A001",
            "建议使用原厂专用油 MIT015027A001",
            "建议使用原厂专用油 MIT025043A001",
            "建议使用原厂专用油 MIT025044A001",
            "建议使用原厂专用油 MIT025045A001",
            "建议使用原厂专用油 MIT025046A001",
            "建议使用原厂专用油 MIZ016236A001",
            "建议使用原厂专用油 MIZ016237A001",
            "建议使用原厂专用油 MIZ026210A001",
            "建议使用原厂专用油 MIZ026213A001",
            "6AT MIM060978A001",
            "建议使用原厂专用油 MIF042150A001",
            "建议使用原厂专用油 MIV041693A001",
            "建议使用原厂专用油 MIK181581A001",
            "建议使用原厂专用油 MIJ013363A001",
            "建议使用原厂专用油 MIG013128A001",
            "建议使用原厂专用油 MIG013128A002",
            "建议使用原厂专用油 MIG013128A003",
            "建议使用原厂专用油 MIL013863A001",
            "建议使用原厂专用油 MIL013864A001",
            "建议使用原厂专用油 MIS170029A001",
            "建议使用原厂专用油 MIS045226A001",
            "建议使用原厂专用油 MIS045227A001",
            "建议使用原厂专用油 MIS045228A001",
            "建议使用原厂专用油 MIS045228A002",
            "75-90 GL-4 MIJ025766A001",
            "75-90 GL-4 MIJ025766A002",
            "建议使用原厂专用油 MIY025585A001",
            "建议使用原厂专用油 MIZ036219A001",
            "建议使用原厂专用油 MIZ046242A001",
            "建议使用原厂专用油 MIZ046243A001",
            "建议使用原厂专用油 MIZ046214A001",
            "建议使用原厂专用油 MIZ046215A001",
            "建议使用原厂专用油 MIZ046244A001",
            "建议使用原厂专用油 MIZ046075A001",
            "建议使用原厂专用油 MIZ046076A001",
            "建议使用原厂专用油 MIZ046077A001",
            "建议使用原厂专用油 MIZ046078A001",
            "5AT MIA010208B001",
            "75-90 GL-4 MIA010208A001",
            "75-90 GL-4 MIA120056B001",
            "6AT MIA080055A001",
            "6AT MIA080059A001",
            "6AT MIA080060A001",
            "6AT MIA080060A006",
            "6AT MIA120058A003",
            "6AT MIA120057A001",
            "7AT MIA080051A001",
            "7AT MIA080051A004",
            "7AT MIA080051A006",
            "6AT MIA120063A001",
            "75-90 GL-4 MIC185856A002",
            "75-90 GL-4 MIL083732A001",
            "75-90 GL-4 MIL123731A001",
            "75-90 GL-4 MIL083733A001",
            "建议使用原厂专用油 MIL123734A001",
            "6AT MIL123741A001",
            "6AT MIL123743A001",
            "6AT MIL083737A001",
            "6AT MIL083740A001",
            "6AT MIL083742A001",
            "6AT MIL083754A001",
            "6AT MIL083751A001",
            "6AT MIL083752B001",
            "6AT MIL083753A001",
            "DCTF MIL083746A001",
            "DCTF MIL083747A001",
            "DCTF MIL083748A001",
            "建议使用原厂专用油 MIR044800A001",
            "建议使用原厂专用油 MIR044800A006",
            "建议使用原厂专用油 MIM184883A001",
            "建议使用原厂专用油 MIM184883A002",
            "建议使用原厂专用油 MIM184883A003",
            "建议使用原厂专用油 MIM184883A004",
            "75-90 GL-4 MIF185631A002",
            "75-90 GL-4 MIF185632A001",
            "75-90 GL-4 MIF185632A002",
            "75-90 GL-4 MIF185632A009",
            "75-90 GL-4 MIF185633A001",
            "75-90 GL-4 MIF185633A002",
            "75-90 GL-4 MIF185633A005",
            "75-90 GL-4 MIF185633A011",
            "75-90 GL-4 MIF185633A014",
            "建议使用原厂专用油 MIF122214A001",
            "建议使用原厂专用油 MIF122209A001",
            "建议使用原厂专用油 MIF122211A001",
            "建议使用原厂专用油 MIK181581A001",
            "5AT MIA010208B001",
            "75-90 GL-4 MIA010208A001",
            "建议使用原厂专用油 MIT025047A001",
            "建议使用原厂专用油 MIT025047A002",
            "建议使用原厂专用油 MID015048A001",
            "建议使用原厂专用油 MID015048A002",
            "建议使用原厂专用油 MID015049A001",
            "建议使用原厂专用油 MID015050A001"
        );
        $sql = 'insert into car_oil_exchange_special(`mi_key`,`demo`) values';
        $values = '';
        foreach( $data as $oil ){
            $item = explode(' ', $oil);
            $mi_key = array_pop($item);
            $oil = implode(' ',$item);

            $values .= !empty( $values ) ? ',' : '';
            $values .= '("' . $mi_key . '","' . $oil . '")';
        }
        $sql .= $values;
//        echo $sql ; die();
        DB::insert( $sql );

    }

    // 更新oil_id
    public function updateData(){

        $number = 0;

        $sql = "SELECT distinct demo FROM car_oil_exchange_special";
        $oil_ex_sp = DB::select( $sql );
        foreach( $oil_ex_sp as $v ){echo '<br/>';print_r( $v);
            $oil_sql = 'select * from car_oil where `name`="' . $v->demo . '"';
            $oils = DB::select( $oil_sql );
            if( !empty( $oils ) ){
                $updateSql = 'update car_oil_exchange_special set oil_id="' . $oils[0]->id . '"'
                    .' where demo="' . $v->demo . '"';
                echo '<br/>';
                echo $updateSql;
                DB::update( $updateSql );
            }

        }
        echo $number;
    }
}
